<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Service
 * @version 		$Id: process.class.php 2738 2011-07-15 10:28:44Z Miguel_Espinoza $
 */
class Connectsocial_Service_Oauth extends Phpfox_Service {

    /**
     * Class constructor
     */
    private $_hybridauth;
    private $_adapter;
    public $session_data;
    public $provider;
    
    private $_aCacheProfile;

    public function __construct($aParams = NULL) {
        $this->_sTable = Phpfox::getT('loginsocial');
    }
    
    /**
     * get config path
     * 
     * @return string config path
     */
    public function getConfigPath() {
        $sDirNameBase = PHPFOX_DIR;
        
        $config = $sDirNameBase . 'hybridauth/config.php';
        
        return $config;
    }
    /**
     * get config value
     * 
     * @return type
     */
    public function getConfig() {
        $config = $this->getConfigPath();
        
        return require($config);
    }
    
    /**
     * Get object HybridAuth
     * 
     * @return object HybridAuth
     */
    public function getHybridAuth() {

        if (!$this->_hybridauth) {
            $sDirNameBase = PHPFOX_DIR;

            $config = $this->getConfigPath();

            require_once( $sDirNameBase . "hybridauth/Hybrid/Auth.php" );

            $this->_hybridauth = new Hybrid_Auth($config);
            
            if(!empty($this->session_data))
                $this->_hybridauth->restoreSessionData($this->session_data);
        }

        return $this->_hybridauth;
    }

    public function getAdapter() {
        
        if (!$this->_adapter && $this->session_data && $this->provider) {
            $this->_adapter = $this->getHybridAuth()->getAdapter($this->provider);
        }

        return $this->_adapter;
    }

    /**
     * 
     * @param type $sProvider
     */
    public function authenticate($sProvider) {
        try {
            $sProvider = @ trim(strip_tags($sProvider));

            //lay adapter. Neu khong co thi redirect sang trang trong file config.
            $this->_adapter = $this->getHybridAuth()->authenticate($sProvider);

            $this->provider = $sProvider;

            $this->session_data = $this->getHybridAuth()->getSessionData();

            $this->_aCacheProfile = $this->getProfile();
        } catch (Exception $e) {
            $error = "";
            switch ($e->getCode()) {
                case 6 : //$error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again."; 
                case 7 : //$error = "User not connected to the provider."; 
                    $this->logout();
                    return $this->authenticate($provider);
                    break;
            }
        }
    }
    /**
     * set handler. Not through authenticate
     * 
     * @param type $aParams
     */
    public function initHandle($aParams) {
        if($aParams && isset($aParams['session_data']) && isset($aParams['provider'])) {
            $this->session_data = $aParams['session_data'];
            $this->provider = $aParams['provider'];
        }
        
        $this->_hybridauth = NULL;
        $this->_adapter = NULL;
    
        $this->_aCacheProfile = NULL;
    }
    
    public function restoreSession($sSessionData) {
        $this->getHybridAuth()->restoreSessionData();
    }

    public function getProfile() {
        if (!$this->_aCacheProfile) {
            $this->_aCacheProfile = $this->getAdapter()->getUserProfile();
        }

        return $this->_aCacheProfile;
    }
    
    /**
     * logout all provider
     */
    public function logout() {
        $this->getHybridAuth()->logoutAllProviders();
    }

}

?>